package com.graduation.community_manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.community_manager.dao.CmUserDao;
import com.graduation.community_manager.entity.CmUserEntity;
import com.graduation.community_manager.service.CmUserService;
import com.graduation.community_manager.vo.PageCmUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;


@Service("cmUserService")
public class CmUserServiceImpl extends ServiceImpl<CmUserDao, CmUserEntity> implements CmUserService {

    @Autowired
    CmUserDao cmUserDao;

    @Override
    public PageInfo<CmUserEntity> cmUserPage(PageCmUserVO pageCmUserVO) {
        QueryWrapper<CmUserEntity> wrapper = new QueryWrapper<>();
        if (pageCmUserVO.getUserName()==null) pageCmUserVO.setUserName("");
        wrapper.like("user_name", pageCmUserVO.getUserName()); // 添加用户名的模糊查询条件
        if (pageCmUserVO.getStartTime() != null) {
            wrapper.between("create_time", pageCmUserVO.getStartTime(), pageCmUserVO.getEndTime()); // 添加注册时间范围的筛选条件
        }
        PageHelper.startPage(pageCmUserVO.getCurrent(),pageCmUserVO.getSize(),true);
        List<CmUserEntity> list = cmUserDao.selectList(wrapper);
        return new PageInfo(list);
    }

    @Override
    public CmUserEntity login(CmUserEntity cmUser) {
        CmUserEntity cmUserEntity = cmUserDao.selectOne(new QueryWrapper<CmUserEntity>().eq("user_name", cmUser.getUserName()));
        if (cmUserEntity != null) {
            if (cmUserEntity.getUserPassword().equals(cmUser.getUserPassword())) return cmUserEntity;
            else throw new RuntimeException("密码不正确");
        }
        throw new RuntimeException("用户不存在");
    }
}